package edu.szu.huhanpeng.doodlingFight.model.data 
{
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessBoardData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.ISquareData;
	/**
	 * 棋盘,组织代表每个方格的SquareData
	 * @author Huhanpeng
	 */
	public class ChessBoardData implements IChessBoardData
	{
		protected var _size:int; 							//棋盘大小为 size*size 的矩形
		
		protected var _rowArr:Array  						//元素为Vector，依次是棋盘上第0，1，2……行
		protected var _columnArr:Array						//元素为Vector，依次是棋盘上第0，1，2……列
		protected var _allVecByRow:Vector.<ISquareData>;	//包含全部方格，按从rowArr式排列。
		
		/////////////////////////////////////////////////////////////////////
		//getter setter                                                    //
		/////////////////////////////////////////////////////////////////////
		public function get size():int
		{
			return _size;
		}
		
		public function get rowArr():Array
		{
			return _rowArr;
		}
		
		public function get columnArr():Array
		{
			return _columnArr;
		}
		
		public function get allVecByRow():Vector.<ISquareData>
		{
			return _allVecByRow;
		}
		/////////////////////////////////////////////////////////////////////
		//构造函数                                                         //
		/////////////////////////////////////////////////////////////////////
		/**
		 * 
		 * @param	size 棋盘大小为 size*size 的矩形
		 */
		public function ChessBoardData( size:int ) 
		{
			init( size );
		}
		
		protected function init(size:int):void 
		{
			_size = size;
			//构造棋盘
			
			//构造_rowArr,_allVecByRow
			//i为行，j为列
			_rowArr = new Array();
			_allVecByRow = new Vector.<ISquareData>(_size*_size,true);
			for (var i:int = 0; i < _size; i++) 
			{
				var rowVec:Vector.<ISquareData> = new Vector.<ISquareData>( _size,true );
				_rowArr[i] = rowVec;
				for (var j:int = 0; j < _size; j++) 
				{
					var squareData:ISquareData = new SquareData(i,j,_size);
					rowVec[j] = squareData;
					_allVecByRow[i * _size + j] = squareData;
				}
			}
			
			//构造_columnArr
			//k为列，l为行
			_columnArr = new Array();
			for (var k:int = 0; k < _size; k++) 
			{
				var columnVec:Vector.<ISquareData> = new Vector.<ISquareData>( _size,true );
				_columnArr[k] = columnVec;
				for (var l:int = 0; l < _size; l++) 
				{
					columnVec[l] = _rowArr[l][k];
				}
			}
			
			//设置每个方格对其上下左右方格的引用，仅此一次
			var rowVec2:Vector.<ISquareData>;
			var columnVec2:Vector.<ISquareData>;
			for (var p:int = 0; p < _size; p++) 
			{
				rowVec2 = _rowArr[p];
				columnVec2 = _columnArr[p];
				for (var q:int = 0; q < _size -1; q++) 
				{
					//关联格子的左右方向
					rowVec2[q].rightSquare = rowVec2[q + 1];
					rowVec2[q + 1].leftSquare = rowVec2[q];				
					
					//关联格子的上下方向
					columnVec2[q].downSquare = columnVec2[q + 1];
					columnVec2[q + 1].upSquare = columnVec2[q];
				}
				
			}
			
			var square:ISquareData;
			for (var m:int = 0; m < _allVecByRow.length; m++) 
			{
				square = _allVecByRow[m];
				
				//设置右下与左上,右上与左下的引用，仅此一次
				if (square.rightSquare != null)
				{
					//右下与左上
					if (square.rightSquare.downSquare != null) 
					{
						square.RDSquare = square.rightSquare.downSquare;
						square.RDSquare.LUSquare = square;
					}
					//右上与左下
					if (square.rightSquare.upSquare != null) 
					{
						square.RUSquare = square.rightSquare.upSquare;
						square.RUSquare.LDSquare = square;
					}
				}
			}
			
		}
		



		
	}

}